package com.googlecode.olabs.gui.client;

import java.util.Collection;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.olabs.gui.client.resources.Card;
import com.googlecode.olabs.gui.client.resources.CardCollection;
import com.googlecode.olabs.gui.client.rest.ResourceObserver;
import com.googlecode.olabs.gui.client.rest.RestResource;

public class MainMenu extends Composite implements HistoryListener,
		ResourceObserver {

	private static final String CARDID = "cardid";
	private ListBox m_cardList;
	private CardDisplay m_cardDisplay;

	private CardCollection m_cardCollection;

	public MainMenu() {
		final HorizontalPanel mainPanel = new HorizontalPanel();
		initWidget(mainPanel);

		final VerticalPanel leftPanel = new VerticalPanel();
		mainPanel.add(leftPanel);

		m_cardDisplay = new CardDisplay();
		mainPanel.add(m_cardDisplay);

		m_cardList = new ListBox();
		leftPanel.add(m_cardList);

		m_cardList.setVisibleItemCount(20);

		leftPanel.add(new Button("Refresh", new ClickListener() {
			public void onClick(Widget sender) {
				m_cardCollection.refreshCards();
			}
		}));
		leftPanel.add(new Button("Load Card", new ClickListener() {
			public void onClick(Widget sender) {
				m_cardCollection.getCard(
						m_cardList.getValue(m_cardList.getSelectedIndex()))
						.load();
			}
		}));

		m_cardList.addChangeListener(new ChangeListener() {
			public void onChange(Widget sender) {
				History.newItem(CARDID + "="
						+ m_cardList.getValue(m_cardList.getSelectedIndex()));
			}
		});

		m_cardCollection = new CardCollection();
		m_cardCollection.addResourceObserver(this);

		History.addHistoryListener(this);
		onHistoryChanged(History.getToken());
	}

	public void resourceChanged(RestResource resource) {
		if (resource == m_cardCollection) {
			m_cardList.clear();
			Collection<Card> cards = m_cardCollection.getCards();
			if (cards.isEmpty()) {
				m_cardList.addItem("No cards");
			} else {
				for (Card card : cards) {
					m_cardList.addItem(card.getCardText(), card.getCardId());
				}
			}
		}
	}

	public void onHistoryChanged(String historyToken) {
		if (historyToken.length() == 0) {
			m_cardDisplay.setCard(null);
		} else if (historyToken.contains("=")) {
			String[] split = historyToken.split("=");
			if (split[0].equals(CARDID)) {
				m_cardDisplay.setCard(m_cardCollection.getCard(split[1]));
			} else {
				m_cardDisplay.setCard(null);
			}
		} else {
			m_cardDisplay.setCard(null);
		}
	}
}
